package com.example.score.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.score.entity.Class;
import com.example.score.entity.Course;
import com.example.score.entity.Student;
import com.example.score.entity.StudentScore;
import com.example.score.service.ClassService;
import com.example.score.service.CourseService;
import com.example.score.service.StudentScoreService;
import com.example.score.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * @author shenrui
 * @date 2021/9/29
 * @description 对学生的成绩的查询，审批等操作
 */
@Controller
public class StudentScoreController {
    @Autowired
    StudentScoreService studentScoreService;
    @Autowired
    StudentService studentService;
    @Autowired
    ClassService classService;
    @Autowired
    CourseService courseService;

    /**
     *@description 查询需要审核的成绩
     * @return String
     */
    @GetMapping("/score_list")
    public String studentManage(HttpSession session) {
        List<StudentScore> studentScoreList = studentScoreService.list(new LambdaQueryWrapper<StudentScore>().eq(StudentScore::getScoreStatus,0));
        Student student;
        Class studentClass;
        Course course;
        for(int i = 0;i < studentScoreList.size();i++) {
            student = studentService.getOne(new LambdaQueryWrapper<Student>().eq(Student::getStuId,studentScoreList.get(i).getStudentId()));
            studentClass =  classService.getOne(new LambdaQueryWrapper<Class>().eq(Class::getId,studentScoreList.get(i).getClassId()));
            if(studentClass != null) {
                course = courseService.getOne(new LambdaQueryWrapper<Course>().eq(Course::getCourseId, studentClass.getCourseId()));
                if(student != null && course != null) {
                    studentScoreList.get(i).setCourseName(course.getCourseName());
                    studentScoreList.get(i).setIdClass(studentClass.getClassId());
                    studentScoreList.get(i).setStuNumber(student.getStuNumber());
                    studentScoreList.get(i).setStuName(student.getStuName());
                }
            }
        }
        session.setAttribute("studentScoreList", studentScoreList);
        return "score_list";
    }

    /**
     *@description 成绩审核通过
     * @return String
     */
    @RequestMapping("/score_pass/{id}")
    public String scorePass(@PathVariable("id") Long id, HttpSession session) {
        StudentScore studentScore = studentScoreService.getById(id);
        if(studentScore != null) {
            if(studentScore.getScoreStatus() == 0) {
                studentScore.setScoreStatus(1L);
                studentScoreService.updateById(studentScore);
            }
        }
        return "redirect:/score_list";
    }

    /**
     *@description 成绩审核失败
     * @return String
     */
    @RequestMapping("/score_no_pass/{id}")
    public String scoreNoPass(@PathVariable("id") Long id, HttpSession session) {
        StudentScore studentScore = studentScoreService.getById(id);
        if(studentScore != null) {
            if(studentScore.getScoreStatus() == 0) {
                studentScore.setScoreStatus(-1L);
                studentScoreService.updateById(studentScore);
            }
        }
        return "redirect:/score_list";
    }
}
